import {
  fetchHome,
  fetchConfig
} from '../../api/home';
import dayjs from 'dayjs';
Page({
  data: {
    imgSrcs: [],
    pageLoading: false,
    current: 0,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: {
      type: 'dots'
    },
    swiperImageProps: {
      mode: 'scaleToFill'
    },
    visible: false,
    mode: '',
    dateVisible: false,
    date: new Date().getTime(), // 支持时间戳传入
    dateText: dayjs(new Date().getTime()).format('YYYY-MM-DD'),
    start: dayjs(new Date().getTime()).add(0, 'day').format('YYYY-MM-DD'),
    end: dayjs(new Date().getTime()).add(10, 'day').format('YYYY-MM-DD'),
    startStation: "外罗",
    endStation: "湛江",
  },

  formSubmit(e) {
    var date = e.detail.value.date; //日期2月2日
    var week = e.detail.value.week; //星期：周四
    wx.navigateTo({
      url: '/pages/train/search/index?startStation=' + this.data.startStation + "&endStation=" + this.data.endStation + "&date=" + this.data.dateText
    });
  },
  onSubmit(e) {
    wx.navigateTo({
      url: "/pages/train/search/index?startStation=&endStation=&date=" + this.data.dateText
    });
  },
  onShow() {
    this.getTabBar().init();
  },
  onLoad() {
    this.init();
  },
  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },
  onPullDownRefresh() {
    this.init();
  },
  init() {
    this.loadHomePage();
  },
  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    fetchHome().then(res => {
      const {
        data,
        total = 0
      } = res.data;
      let imgs = []
      data.forEach(item => {
        imgs.push(item.image)
      })
      this.setData({
        imgSrcs: imgs,
        pageLoading: false,
      });
    });
    fetchConfig().then(res => {
      if (typeof res.data.start !== "undefined" && typeof res.data.end !== "undefined") {
        this.setData({
          startStation: res.data.start,
          endStation: res.data.end,
        });
      }
    })
  },
  showPicker(e) {
    const {
      mode
    } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const {
      mode
    } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const {
      value
    } = e.detail;
    const {
      mode
    } = this.data;
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });

    this.hidePicker();
  },
  bindStartInput(e) {
    this.setData({
      startStation: e.detail.value
    })
  },
  bindEndInput(e) {
    this.setData({
      endStation: e.detail.value
    })
  },
  onColumnChange(e) {

    this.setData({
      dateText: e.detail.value
    })
    console.log('pick', e.detail.value);
  },
  onIconTap() {
    let st = this.data.startStation
    let ed = this.data.endStation
    this.setData({
      startStation: ed,
      endStation: st
    })
  },
  closeNote() {
    this.setData({
      visible: false,
    });
  }
});